﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：Cart.cs
//
//
//文件功能描述：显示具体的商品..
//
//
//创建标识：2007.11.21 王志平
//创建描述：
//
//修改描述:2007.11.30 王志平
 //修改标识:重新修改了翻页方法.
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;


public partial class UserControl_MaterialProduct : System.Web.UI.UserControl
{   
    //
    //调用本地的WebServer
    //
    localhost.ProductService showProducts = new localhost.ProductService();
    //
    //总的页数
    //
    private static int iPageCount;  
    //
    //总的记录数
    //
    private static int iRecords;   

    //private static PagedDataSource pds;
    //
    //定义大类别和小类别的ID
    //
    private static string strLowID;
    private  string strUpperID;
    /// <summary>
    /// 当页面加载时..
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">参数</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //
            //从前个个页面中获取小类别的ID.
            //
            strLowID = Request.QueryString["LowID"].ToString();
            //
            //从小类别的ID中获取大类别的ID.
            //
            strUpperID = strLowID.Substring(0, 7).ToString(); 

            DataBandShow();   
            //
            //当绑定第一页时.
            //
            BindDataList(1);
            //
            //获取页的总数
            //
            GetProductCount();
            //
            //绑定页的选择索引
            //
            BindDropdownListPageIndex();
        }
       
    }

    /// <summary>
    /// 绑定大类别和小类别名.显示的页面的顶部..
    /// </summary>
    /// 
    public void DataBandShow()
    {
        if (strLowID != null)
        {
              string LowName = "";
              string UpperName = "";
            
                //
                //获取小列别的Model从中获取小类别名.
                //
                LowName = showProducts.GetLowerCategoryModel(strLowID).Name.ToString();
                //
                //获取大列别的Model从中获取大类别名.

                //
                UpperName=showProducts.GetUpperCategoryModel(strUpperID).Name.ToString();
                //
                //将大类别和小类别显示.
                //
                Location1.strLabelText = Location1.strLabelText + ">" + UpperName + ">" + LowName;
            

                Page.Title = "商品分类---" + UpperName + "---" + LowName;
         
        }  
    }

    /// <summary>
    /// 数据绑定并调用WebServer的异步调用
    /// </summary>
    /// <param name="iPageIndex">页面的索引</param>
    protected void BindDataList(int iPageIndex)
    {
        //
        //页面大小
        //
        int iPageSize = 8;
        //
        //调用WebServer的异步调用.
        //
        showProducts.GetProductListByCategoryIDCompleted += new localhost.GetProductListByCategoryIDCompletedEventHandler(showProducts_GetProductListByCategoryIDCompleted);
        showProducts.GetProductListByCategoryIDAsync(iPageSize, iPageIndex, strLowID);
        //
        //显示当前页
        //
        lblPage.Text = iPageIndex.ToString();

        if (iPageIndex == 1)
        {
            //
            //如果当前页为第一页
            //
            ddlPage.SelectedValue = "0";
        }//if (iPageIndex == 1)
        else
        {
            ddlPage.SelectedValue = (iPageIndex - 1).ToString();
        }//else

        //
        //显示分页按钮的可用性
        //
        ShowButtonPaging();
        
    }
    /// <summary>
    /// WebServer的异步调用
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">参数</param>
    void showProducts_GetProductListByCategoryIDCompleted(object sender, localhost.GetProductListByCategoryIDCompletedEventArgs e)
    {
            //
            //DataSet保存结果集
           //
            DataSet dsProductList = new DataSet();

            if (e.Result != null)
            {  
                //
                //如果返回结果不空
                //
                dsProductList = e.Result;
                //
                //DataView视图作数据源
                //
                DataView dvProductList = dsProductList.Tables[0].DefaultView;
                //
                //对视图排序,根据下拉列表的选择
                //
                dvProductList.Sort = dropSort.SelectedValue + " ASC";
                //
                //绑定数据源
                //
                dlstLowerProduct.DataSource = dvProductList;
                dlstLowerProduct.DataBind();
            }
    }
    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {
        //
        //计算出记录总数.
        //
        iRecords = showProducts.GetProductCountByCategoryID(strLowID);

        if (iRecords % 8 == 0)
        {  
            //
            //记录数刚好可以整除页大小
            //
            iPageCount = iRecords / 8;
        }
        else
        {
            //
            //记录数不能整除页大小
            //
            iPageCount = Convert.ToInt32(iRecords / 8) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }

    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {
        ListItem lt;
        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            ddlPage.Items.Add(lt);
        }
    }

    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {
        //
        //初始化时分页按钮的可用
        //
        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;

        if (lblPage.Text == "1")
        {
            //如果是第一页，则将首页与上一页的按钮设为不可用
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
            linkbtnLast.Enabled = false;
            linkbtnNext.Enabled = false;
        }
        else if (int.Parse(lblPage.Text) == iPageCount)
        {
            //如果是最后一页，则将末页与下一页设为不可用
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        else
        {
            //其他情况全部为可用
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;

        }
    }
    /// <summary>
    /// 选择页的索引
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }

    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        BindDataList(1);
    }

    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }

    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnPre_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        BindDataList(int.Parse(lblPage.Text));
    }

    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
        BindDataList(iPageCount);
    }

    /// <summary>
    /// 在Datalist中绑定数据..
    /// </summary>
    /// <param name="sender">控件对象</param>
    /// <param name="e">控件参数.</param>
    protected void dlstLowerProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    { 
        //
        //获取Label中绑定的商品的ID.
        //
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;
        //
        //将商品的图片绑定到控件中..
        //
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID;
        //
        //当点击图象时候将连接到ProductInfo页面中去...
        //
        ((ImageButton)e.Item.FindControl("imageBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;
    }
    /// <summary>
    /// 对商品进行排序(价格,名称,时间)
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">参数</param>
    protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataList(int.Parse(lblPage.Text));
    }
}
